#include "Importer.h"
#include "Renderer.h"
//--------------------------------------------------------------------------------
ns::Importer::Importer(Renderer* r)
:
m_pRenderer(r)
{

}
//--------------------------------------------------------------------------------
ns::Importer::~Importer()
{
}
//--------------------------------------------------------------------------------DANI
bool ns::Importer::CreateSprite(char* name, Sprite* sprite)
{
	Sprite* pkSprite = m_pSriteMap[name];

	if(!pkSprite)
		return false;

	pkSprite->Clone(sprite);

	return true;
}
//--------------------------------------------------------------------------------DANI
ns::Texture::Ptr ns::Importer::getTexture(const char* name)
{
	Texture::Ptr pkTexture = m_TextureMap[name];
	Texture::Ptr pkNewTexture(new Texture(*pkTexture));
	return pkNewTexture;
}
//--------------------------------------------------------------------------------
bool ns::Importer::LoadResources(char* filename)
{
	//DANI
	XMLNode kMainNode = XMLNode::openFileHelper(filename,"PMML");

	// load sprites
	int iSpriteCount = kMainNode.nChildNode("SPRITE");	

	int iIterator = 0;
	for(int i=0; i<iSpriteCount; i++)
	{
		XMLNode& rkSprite = kMainNode.getChildNode("SPRITE",&iIterator);
		importSprite(rkSprite);
	}

	return true;
}
//--------------------------------------------------------------------------------dani
bool ns::Importer::importSprite (XMLNode& rkNode)
{
	// load attributes
	const char* pszName = rkNode.getAttribute("Name");

	// find the sprite

	Sprite* pkSprite = m_pSriteMap[pszName];
	
	// if it was found, return
	if(pkSprite)
		return true;

	// create the new sprite
	pkSprite = new Sprite();
	m_pCurrentSprite = pkSprite;

	// add to map
	m_pSriteMap[pszName] = pkSprite;

	// load sprite attributes
	const char* pszTexFile = rkNode.getAttribute("TextureFile");
	const char* pszTexPosX = rkNode.getAttribute("TexPosX");
	const char* pszTexPosY = rkNode.getAttribute("TexPosY");
	const char* pszWidth = rkNode.getAttribute("Width");
	const char* pszHeight = rkNode.getAttribute("Height");

	// convert sprite attibutes
	unsigned int uiTexPosX = atoi(pszTexPosX);
	unsigned int uiTexPosY = atoi(pszTexPosY);
	unsigned int uiWidth = atoi(pszWidth);
	unsigned int uiHeight = atoi(pszHeight);

	// set attributes to sprite
	importTexture(pszTexFile);
	pkSprite->setTexture( m_TextureMap[pszTexFile] );

	pkSprite->setName(pszName);
	pkSprite->setTextureArea(uiTexPosX, uiTexPosY, uiWidth, uiHeight);

	return true;
}
//--------------------------------------------------------------------------------
//dani
bool ns::Importer::importTexture (const char* pszFilename)
{
	assert(m_pRenderer);

	// find the texture
	Texture::Ptr pkTexture = m_TextureMap[pszFilename];
	
	// if it was found, return
	if(pkTexture.get())
		return true;

	// create the new texture
	pkTexture = Texture::Ptr( new Texture(pszFilename) );

	// add to map
	m_TextureMap[pszFilename] = pkTexture;

	m_pRenderer->LoadTexture(pszFilename, pkTexture);

	return true;
}